package com.pengpeng.server.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.PengPengMessage;
import com.pengpeng.server.entity.UserRelationship;

public class UserRelationshipDao{
	
   private DatabaseFactory databasefactory;
	
   public Map<String,UserRelationship> queryByMyphonenum(String myphonenum)
   {
	   RowMapper<UserRelationship> mapper=new RowMapper<UserRelationship>()
		{
			@Override
			public UserRelationship mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String myphonenum="";
				String ifopenmyloc="0";
				String friendphonenum="";
				String iffriendopenloc="0";
				
				myphonenum=rs.getString("myphonenum");
				ifopenmyloc=rs.getString("ifopenmyloc");
				friendphonenum=rs.getString("friendphonenum");
				iffriendopenloc=rs.getString("iffriendopenloc");
		
					
				UserRelationship userrelationship=new UserRelationship();
				userrelationship.setMyphonenum(myphonenum);
				userrelationship.setIfopenmyloc(ifopenmyloc);
				userrelationship.setFriendphonenum(friendphonenum);
				userrelationship.setIffriendopenloc(iffriendopenloc);
				
				return userrelationship;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryUserRelationshipByMyphonenum");
		final Object[] params = new Object[]{myphonenum};
		List<UserRelationship> userrelationshiplist;
	    try {  
	    	userrelationshiplist=jdbctemplate.query(sqlstr, params,mapper);
		   
	        } catch (EmptyResultDataAccessException e) {  
	        	userrelationshiplist=null;  
	    } 
	    if(null!=userrelationshiplist)
	    {
	    	Map<String,UserRelationship> userrelationshipmap=new HashMap<String,UserRelationship>();
	    	userrelationshipmap=Collections.synchronizedMap(userrelationshipmap);
	    	UserRelationship userrelationship;
		    
	    	for(int i=0;i<userrelationshiplist.size();i++)
	    	{
	    		userrelationship=userrelationshiplist.get(i);
	    		userrelationshipmap.put(userrelationship.getFriendphonenum(), userrelationship);
	    	}
	    	return  userrelationshipmap; 
	    }// if(null!=ppmesagelist)
	    else
	    {
	    	return null;
	    }	  //else      
	    	
   }//queryByMyphonenum
   
   public UserRelationship queryByKey(String myphonenum,String friendphonenum)
   {
	   RowMapper<UserRelationship> mapper=new RowMapper<UserRelationship>()
		{
			@Override
			public UserRelationship mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String myphonenum="";
				String ifopenmyloc="0";
				String friendphonenum="";
				String iffriendopenloc="0";
				
				myphonenum=rs.getString("myphonenum");
				ifopenmyloc=rs.getString("ifopenmyloc");
				friendphonenum=rs.getString("friendphonenum");
				iffriendopenloc=rs.getString("iffriendopenloc");
		
					
				UserRelationship userrelationship=new UserRelationship();
				userrelationship.setMyphonenum(myphonenum);
				userrelationship.setIfopenmyloc(ifopenmyloc);
				userrelationship.setFriendphonenum(friendphonenum);
				userrelationship.setIffriendopenloc(iffriendopenloc);
				
				return userrelationship;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryUserRelationshipByKey");
		final Object[] params = new Object[]{myphonenum,friendphonenum};
		UserRelationship userrelationship;
	    try {  
	    	userrelationship=jdbctemplate.queryForObject(sqlstr,params,mapper);
		   return userrelationship;
	        } catch (EmptyResultDataAccessException e) {  
	       return null;  
	    } 
	    	
   }//queryByKey
   
   public boolean insert(UserRelationship userrelationship)
   {
	    Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String myphonenum=userrelationship.getMyphonenum();
		String ifopenmyloc=userrelationship.getIfopenmyloc();
		String friendphonenum=userrelationship.getFriendphonenum();
		String iffriendopenloc=userrelationship.getIffriendopenloc();

		Object[] params = new Object[] {myphonenum,ifopenmyloc,friendphonenum,iffriendopenloc};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertUserRelationship");
		jdbctemplate.update(sqlstr,params);		
		return true;
   }//insert
   public boolean update(UserRelationship userrelationship)
   {
	   Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String myphonenum=userrelationship.getMyphonenum();
		String ifopenmyloc=userrelationship.getIfopenmyloc();
		String friendphonenum=userrelationship.getFriendphonenum();
		String iffriendopenloc=userrelationship.getIffriendopenloc();

		Object[] params = new Object[] {myphonenum,ifopenmyloc,friendphonenum,iffriendopenloc,myphonenum,friendphonenum};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateUserRelationship");
		jdbctemplate.update(sqlstr,params);		
		return true;
   }//update
   
   public boolean delete(UserRelationship userrelationship)
   {
	    Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {userrelationship.getMyphonenum(),userrelationship.getFriendphonenum()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteUserRelationship");
		jdbctemplate.update(sqlstr,params);	
		return true;
   }//delete

public DatabaseFactory getDatabasefactory() {
	return databasefactory;
}

public void setDatabasefactory(DatabaseFactory databasefactory) {
	this.databasefactory = databasefactory;
}
   
}
